package com.hankkin.reading.control

import com.zhsm.paipaiout.baselibrary.utils.SPUtils
import com.zhsm.fuxi.bean.OrderResp

object OrderControl {
    const val ORDER = "order"
    private var order: OrderResp? = null

    fun setCurrentOrder(nowOrder: OrderResp) {
        this.order = nowOrder
        order?.let {
            saveOrderSp(it)
        }
    }

    fun updateOrderMianInfo(nowOrder: OrderResp) {
       val nOrder =  getCurrentOrder()
        nOrder?.id = nowOrder.id
        nOrder?.node = nowOrder.node
        nOrder?.state = nowOrder.state
        nOrder?.amount = nowOrder.amount
        nOrder?.expireMillis = nowOrder.expireMillis
        nOrder?.let {
            saveOrderSp(it)
        }
    }
    fun updateOrderMianAndVehicleInfo(nowOrder: OrderResp) {
       val nOrder =  getCurrentOrder()
        nOrder?.id = nowOrder.id
        nOrder?.node = nowOrder.node
        nOrder?.state = nowOrder.state
        nOrder?.amount = nowOrder.amount
        nOrder?.expireMillis = nowOrder.expireMillis
        nOrder?.vehicle = nowOrder.vehicle
        nOrder?.let {
            saveOrderSp(it)
        }
    }
    fun getCurrentOrder(): OrderResp? {
        if (this.order == null) {
            this.order = SPUtils.getObject(ORDER, OrderResp::class.java) as OrderResp
        }
        return this.order
    }

    private fun saveOrderSp(nowOrder: OrderResp) {
        SPUtils.saveObject(ORDER, nowOrder)
//        LogUtils.d(ORDER + userBean.id)
    }
    fun getId(): Long {
        if (this.order == null) {
            this.order = SPUtils.getObject(ORDER, OrderResp::class.java) as OrderResp
        }
        return if (order?.id != null) {
            order?.id!!
        } else -1
    }
    fun clearOrder() {
        this.order = null
        SPUtils.remove( ORDER)
    }
}
